package com.sig.util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * MD5工具
 *
 * @author sig
 * @since 2019/1/26
 */
public final class Md5Util {

    private Md5Util() {
    }

    public static String md5EncryptBy16(final String message) {
        String encrypt = md5EncryptBy32(message);
        return null == encrypt ? null : encrypt.substring(8, 24);
    }

    public static String md5EncryptBy32(final String message) {
        String encrypt = "";
        try {
            final StringBuilder builder = new StringBuilder();
            final MessageDigest digest = MessageDigest.getInstance("MD5");
            final byte[] hash = digest.digest(message.getBytes(StandardCharsets.UTF_8));
            for (byte tmp : hash)
                builder.append(String.format("%02x", tmp & 0xff));
            encrypt = builder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return encrypt;
    }
}
